package com.gdglc.spring.biz.impl.auth;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gdglc.spring.biz.auth.FunctionauthorityBiz;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.y2.authprj.bean.auth.model.FunctionauthorityInfo;
import com.y2.authprj.bean.auth.model.StaffInfo;
import com.y2.authprj.dao.auth.IFunctionauthorityDao;
import com.y2.authprj.exception.BizException;

public class FunctionauthorityBizImpl implements FunctionauthorityBiz{

	private IFunctionauthorityDao dao;
	
	public IFunctionauthorityDao getDao() {
		return dao;
	}

	public void setDao(IFunctionauthorityDao dao) {
		this.dao = dao;
	}

	@Override
	public List<FunctionauthorityInfo> findAll() throws BizException {
		
		return dao.findAll();
	}

	@Override
	public List<FunctionauthorityInfo> findAllByParentId(Integer parentId) {
		
		return dao.findAllByParentId(parentId);
	}
	@Override
	public void add(FunctionauthorityInfo fInfo) throws BizException{
			if(this.findAllByParentIdAndName(fInfo)==0){
				//默认是正常
				fInfo.setDeleteFlag(1);
				//创建时间是当前时间
				fInfo.setCreateTime(new Date());
				try {
					dao.addFunctionauthority(fInfo);
				} catch (Exception e) {
					throw new BizException("添加失败",e);
				}
			}
		}


	@Override
	public void del(Integer id) throws BizException{
		if(null == id||id==0){
			throw new BizException("删除错误，请指定需要删除的记录");
		}
		try {
			dao.deleteFunctionauthority(id);
		} catch (Exception e) {
			throw new BizException("删除失败",e);
		}
	}

	@Override
	public List<FunctionauthorityInfo> findAllByName(String name) throws BizException{
		List<FunctionauthorityInfo> functionauthorityInfos=null;
		try{
			functionauthorityInfos=dao.findAllByName(name);
		}catch(Exception e){
			throw new BizException("查询失败");
		}
		if(functionauthorityInfos.size()==0){
			throw new BizException("没有此数据的存在");
		}
		
		return functionauthorityInfos;
	}


	@Override
	public List<FunctionauthorityInfo> queryByChose(FunctionauthorityInfo functionauthorityInfo) {
		return dao.queryByChose(functionauthorityInfo);
	}

	@Override
	public PageInfo<FunctionauthorityInfo> queryByPage(FunctionauthorityInfo functionauthorityInfo, Integer pageNo,
			Integer pageSize) {
		pageNo = pageNo == null?1:pageNo;//当前页  *必须
	    pageSize = pageSize == null?10:pageSize;//每页的数量* 必须
	    PageHelper.startPage(pageNo, pageSize);//*必须
	    List<FunctionauthorityInfo> list = dao.queryByChose(functionauthorityInfo);
	    PageInfo<FunctionauthorityInfo> page = new PageInfo<FunctionauthorityInfo>(list);
	    return page;
	}

	@Override
	public PageInfo<FunctionauthorityInfo> findAllByName(String name, Integer pageNo, Integer pageSize){
		
		pageNo = pageNo == null?1:pageNo;//当前页  *必须
	    pageSize = pageSize == null?10:pageSize;//每页的数量* 必须
	    PageHelper.startPage(pageNo, pageSize);//*必须
	    List<FunctionauthorityInfo> list=null;
		try {
			list = this.findAllByName(name);
		} catch (BizException e) {
			System.out.println(e.getMessage());
		}
	    PageInfo<FunctionauthorityInfo> page = new PageInfo<FunctionauthorityInfo>(list);
	    return page;
	}

	@Override
	public PageInfo<FunctionauthorityInfo> findAllByParentId(Integer parentId, Integer pageNo, Integer pageSize) {
		
		pageNo = pageNo == null?1:pageNo;//当前页  *必须
	    pageSize = pageSize == null?10:pageSize;//每页的数量* 必须
	    PageHelper.startPage(pageNo, pageSize);//*必须
	    List<FunctionauthorityInfo> list = dao.findAllByParentId(parentId);
	    PageInfo<FunctionauthorityInfo> page = new PageInfo<FunctionauthorityInfo>(list);
	    return page;
	}

	@Override
	public int findAllByParentIdAndName(FunctionauthorityInfo functionauthorityInfo) {
		
		return dao.findAllByParentIdAndName(functionauthorityInfo);
		
	}

	@Override
	public List<Integer> findFunctionauthorityIdByStaffId(int staffId) {
		
		return dao.findFunctionauthorityIdByStaffId(staffId);
	}
	
}
